define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            //点击时间选择变化
            $('#consume_date').change(function(){
                param = '&month='+$(this).val();
                window.location.href = (location_url + param);
            });

            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'card/consumecard/index' + param,
                    // add_url: 'card/consumecard/add',
                    // edit_url: 'card/consumecard/edit',
                    // del_url: 'card/consumecard/del',
                    // multi_url: 'card/consumecard/multi',
                    table: 'consume_card',
                }
            });

            if(type == 0){//超级管理员
                var arr = [
                    {checkbox: true},
                    {field: 'id', title: __('Id')},
                    {field: 'nickname', title: __('用户名')},
                    {field: 'avatar', title: __('用户头像'),formatter: Table.api.formatter.image},
                    {field: 'send_store_name',title: __('卡券发送方')},
                    {field: 'title', title: __('卡券名称')},
                    {field:'reduce_cost',title: __('卡券优惠金额')},
                    {field: 'code', title: __('卡券code')},
                    {field: 's_name', title: __('核销店铺名')},
                    {field: 'cancel_nickname', title: __('核销员')},
                    {field: 'cancel_avatar', title: __('核销员头像'),formatter: Table.api.formatter.image},
                    {field: 'createtime', title: __('Createtime'), formatter: Table.api.formatter.datetime},
                ]
            }else if(type == 1){//商家
                var arr = [
                    {checkbox: true},
                    {field: 'id', title: __('Id')},
                    {field: 'nickname', title: __('用户名')},
                    {field: 'avatar', title: __('用户头像'),formatter: Table.api.formatter.image},
                    {field:'reduce_cost',title: __('卡券优惠金额')},
                    {field: 'code', title: __('卡券code')},
                    {field: 's_name', title: __('核销店铺名')},
                    {field: 'cancel_nickname', title: __('核销员')},
                    {field: 'cancel_avatar', title: __('核销员头像'),formatter: Table.api.formatter.image},
                    {field: 'createtime', title: __('Createtime'), formatter: Table.api.formatter.datetime},
                ]
            }else{//广告商
                var arr = [
                    {checkbox: true},
                    {field: 'id', title: __('Id')},
                    {field: 'nickname', title: __('用户名')},
                    {field: 'avatar', title: __('用户头像'),formatter: Table.api.formatter.image},
                    {field: 'send_store_name',title: __('卡券发送方')},
                    {field: 'title', title: __('卡券名称')},
                    {field:'reduce_cost',title: __('卡券优惠金额')},
                    {field: 'code', title: __('卡券code')},
                    {field: 'createtime', title: __('Createtime'), formatter: Table.api.formatter.datetime},
                ]
            }

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [arr]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});